


// 初始化安装模块
void Install_Init()
{
	str sFile = xrtPathJoin(2, AppPath, "install.lock");
	G_Install = xrtFileExists(sFile);
	xrtFree(sFile);
}



// 卸载安装模块
void Install_Unit()
{
	
}



// 安装 xPanel 请求
void Request_Install(XS_ServerObject objServer, XS_HostObject objHost, struct mg_connection* c, struct mg_http_message* hm)
{
	if ( hm->methodCode == HTTP_GET ) {
		
		// 安装页面
		LoadPage(c, 200, HTTP_CT_HTML, "install.html");
		
	} else if ( hm->methodCode == HTTP_POST ) {
		
		// 安装请求
		G_Install = TRUE;
		
		// step 1 : 复制数据库 + 创建连接
		str sSrcDB = xrtPathJoin(2, InstallPath, "main.db");
		str sDstDB = xrtPathJoin(2, DBPath, "main.db");
		xrtFileCopy(sSrcDB, sDstDB, TRUE);
		xrtFree(sSrcDB);
		xrtFree(sDstDB);
		void DB_Init();
		DB_Init();
		
		// step 2 : 创建超管账号
		xvalue tblForm = xrtParseJSON(hm->body.buf, hm->body.len);
		str sUser = xvoTableGetText(tblForm, "username", 8);
		str sPwd = xvoTableGetText(tblForm, "password", 8);
		mg_md5_ctx ctx;
		mg_md5_init(&ctx);
		mg_md5_update(&ctx, sUser, strlen(sUser));
		mg_md5_update(&ctx, "_xPanel_", 8);
		mg_md5_update(&ctx, sPwd, strlen(sPwd));
		uint8 buf[16];
		mg_md5_final(&ctx, buf);
		str sPwdMD5 = xrtHexEncode(buf, 16);
		xtime tNow = xrtNow();
		str sSQL = xrtFormat("INSERT INTO user (role, user, pwd, createTime, updateTime, isDelete) VALUES (1, '%s', '%s', %lld, %lld, 0)", sUser, sPwdMD5, tNow, tNow);
		xdoExecute(G_DB, sSQL);
		xrtFree(sPwdMD5);
		xrtFree(sSQL);
		xvoUnref(tblForm);
		
		// step 3 : 创建安装锁定文件
		str sFile = xrtPathJoin(2, AppPath, "install.lock");
		xrtFilePutAll(sFile, "xPanel installed", 16);
		xrtFree(sFile);
		
		// step 4 : 返回安装成功响应
		http_reply(c, 200, HTTP_CT_JSON, "{\"result\": true, \"msg\": \"xPanel 服务器管理面板安装成功！\"}", 0);
		
	} else {
		
		// 其他请求方法返回 404 页面
		LoadPage(c, 404, HTTP_CT_HTML, "status/404.html");
		
	}
}


